﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Windows.Zip;

namespace ZenHR.AttData.Package.Util
{
    public static class ZipZenHRHepler
    {
        /// <summary>
        /// 创建压缩文件
        /// </summary>
        /// <param name="filePath">ZIP文件路径</param>
        /// <param name="files">子文件集合 key file Name in Zip , value file path.</param>
        public static void CreateZipFile(string zipfilePath, Dictionary<string, string> files)
        {
            var s = new Stopwatch();
            s.Start();
            MemoryStream memStream = new MemoryStream();
           // Utility.Tools.Loger.WriteMsgToLog("开始压缩：" + zipfilePath + " 子文件数量：" + files.Count);
            using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create, true, null))
            {
                var index = 1;
                foreach (var fileName in files.Keys)
                {
                    //Utility.Tools.Loger.WriteMsgToLog("正在压缩：" + zipfilePath + " " + index + "/" + files.Count);
                    using (ZipArchiveEntry entry = archive.CreateEntry(fileName))
                    {
                        BinaryWriter writer = new BinaryWriter(entry.Open());
                        writer.Write(File.ReadAllBytes(files[fileName]));
                        writer.Flush();
                    }
                    index++;
                }
            }
            memStream.Seek(0, SeekOrigin.Begin);
            File.WriteAllBytes(zipfilePath, StreamToBytes(memStream));
            s.Stop();
           // Utility.Tools.Loger.WriteMsgToLog("完成压缩：" + zipfilePath + " 子文件数量：" + files.Count + " 完成耗时(s)：" + s.ElapsedMilliseconds / 1000.00);
        }

        /// <summary>
        /// 创建压缩文件
        /// </summary>
        /// <param name="zipfilePath">ZIP文件路径</param>
        /// <param name="fileName">压缩文件中显示的文件名</param>
        /// <param name="filePath">被压缩的文件绝对路径</param>
        public static void CreateZipFile(string zipfilePath, string fileName, string filePath)
        {
            var dic = new Dictionary<string, string>();
            dic.Add(fileName, filePath);
            CreateZipFile(zipfilePath, dic);
        }
        public static byte[] StreamToBytes(Stream stream)
        {
            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        // Fields

        /// <summary>
        /// 压缩文件夹目录
        /// </summary>
        /// <param name="strInDirectoryPath">指定需要压缩的目录，如C:\test\，将压缩test目录下的所有文件</param>
        /// <param name="strOutFilePath">压缩后压缩文件的存放目录 如: c\test2\test.7z</param>
        public static void CompressDirectory(string strInDirectoryPath, string strOutFilePath)
        {
            var _7zInstallPath = AppDomain.CurrentDomain.BaseDirectory + @"/7z/7z.exe";
            if (!System.IO.Directory.Exists(strInDirectoryPath))
            {
                throw new Exception("指定的压缩目录不存在!");
            }
            Process process = new Process();
            process.StartInfo.FileName = _7zInstallPath;
            process.StartInfo.Arguments = " a " + strOutFilePath + " " + strInDirectoryPath + @"\*";
            //隐藏DOS窗口
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.Start();
            process.WaitForExit();
            process.Close();
        }

        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="strInFilePath">指定需要压缩的文件，如C:\test\demo.xlsx，将压缩demo.xlsx文件</param>
        /// <param name="strOutFilePath">压缩后压缩文件的存放目录</param>
        public static void CompressFile(string strInFilePath, string strOutFilePath)
        {
            var _7zInstallPath = AppDomain.CurrentDomain.BaseDirectory + @"/7z/7z.exe";
            Process process = new Process();
            process.StartInfo.FileName = _7zInstallPath;
            //process.StartInfo.Arguments = " a -t7z " + strOutFilePath + " " + strInFilePath + "";
            process.StartInfo.Arguments = " x " + strOutFilePath + @" -o" + strInFilePath + " *.*";
            //隐藏DOS窗口
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.Start();
            process.WaitForExit();
            process.Close();
            process.Dispose();
        }

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="strInFilePath">压缩文件的路径</param>
        /// <param name="strOutDirectoryPath">解压缩后文件的路径</param>
        public static void DecompressFileToDestDirectory(string strInFilePath, string strOutDirectoryPath)
        {
            var _7zInstallPath = AppDomain.CurrentDomain.BaseDirectory + @"\7z\7z.exe";
            if (!File.Exists(_7zInstallPath))
            {
                _7zInstallPath = AppDomain.CurrentDomain.BaseDirectory + @"\7z.exe";
            }
            Process process = new Process();
            process.StartInfo.FileName = _7zInstallPath;
            process.StartInfo.Arguments = " x \"" + strInFilePath + "\" -o\"" + strOutDirectoryPath + "\" -y ";
            //隐藏DOS窗口
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.Start();
            process.WaitForExit();
            process.Close();
            process.Dispose();
        }
    }
}